package pixelradar;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.internal.NetworkModHolder;
import net.minecraftforge.fml.relauncher.CoreModManager;
import pixelradar.util.Utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class FuckYouFerris {
    public static void ProcessEvasion() {
        try {
            ModScanner();
            ModList();
            ActiveModList();
            NetworkRegistry();
        } catch (Exception var1) {
            var1.printStackTrace();
        }

    }

    private static void ModScanner() throws NoSuchFieldException, IllegalAccessException {
        Field field = CoreModManager.class.getDeclaredField("ignoredModFiles");
        field.setAccessible(true);
        List<String> ignore = Utils.castList(field.get(CoreModManager.class), String.class);
        List<String> ignoreCopy = new ArrayList<>(ignore);
        ignoreCopy.add("pixelradar-5.4.jar");
        ignoreCopy.add("pixelradar.jar");
        field.set(FMLCommonHandler.instance(), ImmutableList.copyOf(ignoreCopy));
        field.setAccessible(false);
    }

    private static void ModList() throws NoSuchFieldException, IllegalAccessException {
        Field mods = Loader.instance().getClass().getDeclaredField("mods");
        mods.setAccessible(true);
        List<ModContainer> modsList = Utils.castList(mods.get(Loader.instance()), ModContainer.class);
        List<ModContainer> modsListTmp = new ArrayList<>();

        for (ModContainer modContainer : modsList) {
            if (!"pixelradar".equals(modContainer.getModId())) {
                modsListTmp.add(modContainer);
            }
        }

        mods.set(Loader.instance(), ImmutableList.copyOf(modsListTmp));
        mods.setAccessible(false);
    }

    private static void ActiveModList() {
        List<ModContainer> modListActive = Loader.instance().getActiveModList();
        new ArrayList<>(modListActive);
        int thismod = 0;

        for (int i = 0; i < modListActive.size(); ++i) {
            if ("pixelradar".equals(modListActive.get(i).getModId())) {
                thismod = i;
            }
        }

        modListActive.remove(modListActive.get(thismod));
    }

    private static void NetworkRegistry() throws NoSuchFieldException, IllegalAccessException {
        Field registryField = NetworkRegistry.INSTANCE.getClass().getDeclaredField("registry");
        registryField.setAccessible(true);
        Map registry = (Map) registryField.get(NetworkRegistry.INSTANCE);
        Map<ModContainer, NetworkModHolder> registryTmp = new HashMap<>();

        for (Object o : registry.entrySet()) {
            Entry networkMod = (Entry) o;
            if (!"pixelradar".equals(((ModContainer) networkMod.getKey()).getModId())) {
                registryTmp.put((ModContainer) networkMod.getKey(), (NetworkModHolder) networkMod.getValue());
            }
        }

        registryField.set(NetworkRegistry.INSTANCE, ImmutableMap.copyOf(registryTmp));
        registryField.setAccessible(false);
    }

    private static void addBranding() {
        try {
            Field brdfield = FMLCommonHandler.instance().getClass().getDeclaredField("brandings");
            brdfield.setAccessible(true);
            List<String> brd = Utils.castList(brdfield.get(FMLCommonHandler.instance()), String.class);
            List<String> brdCopy = new ArrayList<>(brd);
            brdCopy.add("PixelRadar v5.4 by wujichen158");
            brdfield.set(FMLCommonHandler.instance(), ImmutableList.copyOf(brdCopy));
            brdfield.setAccessible(false);
        } catch (IllegalAccessException | NoSuchFieldException var3) {
            var3.printStackTrace();
        }

    }
}
